use super::{CRece, Client, RNull};
use std::time::Instant;
use tracing::{instrument, warn};

#[instrument(skip_all, name = "queue_chunk")]
pub async fn queue_chunk(rx: &CRece, client: &Client) {
    loop {
        let _ = do_que(rx, client).await;
    }
}

#[instrument(skip_all, name = "do_que")]
async fn do_que(rx: &CRece, client: &Client) -> RNull {
    if let Ok(d) = rx.recv().await {
        warn!("queue len: {:?}", rx.len());

        let start = Instant::now();

        let ret = client.batch_que(d);

        let duration = start.elapsed();

        match ret {
            Ok(_) => {
                warn!("queue time: {:?}", duration);
            }
            Err(e) => {
                warn!("queue time: {:?}, queue error: {:?}", duration, e);
            }
        }
    }

    Ok(())
}
